﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class addEditChargeModule : Form
    {
        editEmployeeRecordsModule parent;
        TextBox amnt; //amount
        String val;
        int type;
        int typeOfCharge;
        public addEditChargeModule(editEmployeeRecordsModule parent, int type, int typeOfCharge)
        {
            this.parent = parent;
            this.type = type;
            this.typeOfCharge = typeOfCharge;
            InitializeComponent();
            amnt = (TextBox)addEditChargeAmountTextBox.Controls[1];
            amnt.TextChanged += new EventHandler(amnt_TextChanged);
            amnt.Validating += new CancelEventHandler(amnt_Validating);
            val = amnt.Text;

            if (type == 1)
            {
                addEditChargeDatePicker.Value = DateTime.Parse(parent.getChargeSelectedDate(typeOfCharge));
                addEditChargeAmountTextBox.Value = parent.getChargeAmount(typeOfCharge);
                addEditChargeDescriptionTextBox.Text = parent.getChargeDescription(typeOfCharge);
            }
        }

        private void addEditChargeCancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addEditChargeOkButton_Click(object sender, EventArgs e)
        {
            Charge charge = new Charge();
            charge.Date= addEditChargeDatePicker.Value.ToString("yyyy-MM-dd");
            charge.Amount= ((double)addEditChargeAmountTextBox.Value);
            charge.Description = addEditChargeDescriptionTextBox.Text;
            charge.firstName = parent.getSelectedFirstName();
            charge.lastName = parent.getSelectedLastName();
            if (!(DateTime.Compare(addEditChargeDatePicker.Value, DateTime.Now) > 0))
            {
                if (type == 0)
                {
                    bool success = new ChargeController().addCharge(charge, typeOfCharge);
                    if (success)
                    {
                        parent.initializeContent();
                        this.Close();
                    }
                }
                else
                {
                    DateTime date = DateTime.Parse(parent.getChargeSelectedDate(typeOfCharge));
                    double amount = ((double)parent.getChargeAmount(typeOfCharge));
                    string description = parent.getChargeDescription(typeOfCharge);
                    bool success = new ChargeController().editCharge(charge, typeOfCharge, date.ToString("yyyy-MM-dd"), amount, description);
                    if (success)
                    {
                        parent.initializeContent();
                        this.Close();
                    }
                }
            }
            else
                MessageBox.Show("Date cannot be set a future date", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void amnt_TextChanged(object sender, EventArgs e)
        {
            if (amnt.Focused)
                val = amnt.Text;
        }

        private void amnt_Validating(object sender, CancelEventArgs e)
        {
            if (multipleDecPoints(val))
            {
                MessageBox.Show("Invalid input; too many decimal points.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                if (val.Equals("."))
                {
                    MessageBox.Show("Invalid input.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    double check = Double.Parse(val);
                    if (check < (double)addEditChargeAmountTextBox.Minimum || check > (double)addEditChargeAmountTextBox.Maximum)
                        MessageBox.Show("Invalid input; out of range.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
        }

        private bool multipleDecPoints(String num)
        {
            int nPoints = 0;
            int i = 0;
            while (nPoints < 2 && i < num.Length)
            {
                if (num[i] == '.')
                    nPoints++;
                i++;
            }
            if (nPoints < 2)
                return false;
            else
                return true;
        }
    }
}
